﻿using System;
using System.Collections.Generic;
using System.Text;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using YArchitech.Revit;
using YJKGeometryFuncs;

namespace YJKAreaStatistic.Manage
{
	internal class AreaParamManager
	{
		public static bool GetAreaHouseName(Area area, ref string strHouseName)
		{
			try
			{
				Parameter parameter = area.GetParameter(BuiltInParameter.ROOM_NAME);
				if (parameter != null)
				{
					strHouseName = parameter.AsString();
				}
			}
			catch (Exception)
			{
				return false;
			}
			return true;
		}

		public static bool GetAreaRooms(Area area, ref List<Room> lstRoom)
		{
			try
			{
				List<ElementId> list = new List<ElementId>();
				InsertDataManager.ReadExtensibleStorageFromArea(area, ref list);
				foreach (ElementId elementId in list)
				{
					Room room = area.Document.GetElement(elementId) as Room;
					if (room != null)
					{
						lstRoom.Add(room);
					}
				}
			}
			catch (Exception)
			{
				return false;
			}
			return true;
		}

		public static double GetAreaAreaValue(Area area)
		{
			double result = 0.0;
			try
			{
				result = area.GetParameter(BuiltInParameter.ROOM_AREA).AsValueString().ToDouble(0.0);
			}
			catch (Exception)
			{
				return 0.0;
			}
			return result;
		}

		public static bool IsRoomHalfRegion(Room rm)
		{
			if (rm == null)
			{
				return false;
			}
			try
			{
				Parameter parameter = rm.GetParameter("面积系数");
				double val = 0.0;
				if (parameter != null)
				{
					if ((int)parameter.StorageType == 2)
					{
						val = AssistFunc.feetToMM(rm.GetParameter("面积系数").AsDouble());
					}
					else if ((int)parameter.StorageType == 3)
					{
						val = Convert.ToDouble(rm.GetParameter("面积系数").AsString());
					}
					if (Geometry.IsEqual(val, 0.5, 0.1))
					{
						return true;
					}
				}
			}
			catch (Exception)
			{
				return false;
			}
			return false;
		}
	}
}
